package com.develope.tree.image;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.develope.tree.BaseApplication;
import com.develope.tree.R;

public class TakePicture extends Activity {

	static final int REQUEST_IMAGE_CAPTURE = 1;
	private static final int CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE = 100;

	private String mCurrentPhotoPath,user;
	private Bitmap imageBitmap;
	private ImageView mImageView;
	private EditText lo, cay;
	private Button bt_save;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.layout_preview);

		mImageView = (ImageView) findViewById(R.id.imgdisplay);
		lo = (EditText) findViewById(R.id.edtlo);
		cay = (EditText) findViewById(R.id.edtcay);

		Intent i = getIntent();
		Bundle extras = i.getExtras();
		if(extras!=null){
			user = extras.getString("user");
			return;
		}
		
		bt_save = (Button) findViewById(R.id.Bt_save);
//		bt_save.setOnClickListener(new OnClickListener() {
			
//			public void onClick(View v) {
				// TODO Auto-generated method stub
//				dispatchTakePictureIntent();
//			}

//		});
		

	}

	private File creatImageFile() throws IOException {
		String timeStamp = new SimpleDateFormat("yyyyMMddHHmmss")
				.format(new Date());
		String imageFileName = "JPEG_" + timeStamp + "_";
		File storageDir = Environment
				.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
		File image = File.createTempFile(imageFileName, ".jpg", storageDir);

		mCurrentPhotoPath = "file:" + image.getAbsolutePath();
		return image;
	}

	private void dispatchTakePictureIntent() {
		Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
		if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
			File photoFile = null;
			try {
				photoFile = creatImageFile();
			} catch (IOException ex) {
				BaseApplication.makeToastMsg("Cannot Save Photo");
			}

			if (photoFile != null) {
				takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT,
						Uri.fromFile(photoFile));

				startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
			}
		}
	}

	protected void onActivityResult(int requestCode, int resultCode, Intent data) {

		if (requestCode == REQUEST_IMAGE_CAPTURE ) {
			if (resultCode == RESULT_OK) {
					
			new LoadImagesFromSFCard().execute(mCurrentPhotoPath);
			}
			
			else if ( resultCode == RESULT_CANCELED) {                
                Toast.makeText(this, " Picture was not taken ", Toast.LENGTH_SHORT).show();
                }
			else {
				Toast.makeText(this, " Picture was not taken ", Toast.LENGTH_SHORT).show();
			}
//			Bundle extras = data.getExtras();
//			Bitmap imageBitmap = (Bitmap) extras.get("data");
//			mImageView.setImageBitmap(imageBitmap);
			

			super.onActivityResult(requestCode, resultCode, data);

		}
	}
	
	public class LoadImagesFromSFCard extends AsyncTask<String, Void, Void> {

		private ProgressDialog dialog = new ProgressDialog(TakePicture.this);
		Bitmap mbitmap;
		
		protected void onPreExcute() {
//			Alerts.showLoadingDialog(TakePicture.this, " Loading image from Sdcard..", cancelListener());
			dialog.setMessage(" Loading image from Sdcard..");
            dialog.show();
		}
		private OnClickListener cancelListener() {
			// TODO Auto-generated method stub
			return null;
		}
		@Override
		protected Void doInBackground(String... urls) {
			// TODO Auto-generated method stub
			Bitmap bitmap = null;
			Bitmap newBitmap = null;
			Uri uri = null;
			
			try {
				Uri.withAppendedPath(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "" + urls[0]);
				bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(uri));
				
				if (bitmap != null){
					newBitmap = Bitmap.createScaledBitmap(bitmap, 170, 170, true);
					bitmap.recycle();
					if (newBitmap != null){
						mbitmap = newBitmap;
					}
				}
			} catch (IOException e){
				cancel(true);
			}
			return null;
		}
		
		protected void onPostExcute(Void unused) {
			dialog.dismiss();
			
			if(mbitmap != null){
				mImageView.setImageBitmap(mbitmap);
			}
		}
		
	}

}
